package cn.cloud9.opencloud.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

/**
 * @author OnCloud9
 * @description
 * @project Open-Cloud
 * @date 2022年04月16日 15:25
 */
@Configuration
public class ErrorPageConfig {

    @Value("${error.page.path:common/error}")
    private String errorPagePath;
    @Value("${api.path:api}")
    private String apiPath;

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        String basePath = "/" +  apiPath + "/" + errorPagePath;
        return factory -> {
            final ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND, basePath + "/404");
            final ErrorPage errorPage405 = new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, basePath + "/405");
            final ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, basePath + "/500");
            factory.addErrorPages(errorPage404, errorPage405, errorPage500);
        };
    }
}
